home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / diff_2_1.lha / diff-2.1 / side.c < prev    next >
C/C++ Source or Header  |  1993-01-31  |  7KB  |  284 lines

  1. /* sdiff-format output routines for GNU DIFF.
  2.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "diff.h"
  23.  
  24.  
  25. static void print_sdiff_hunk ();
  26. static void print_sdiff_common_lines ();
  27. static void print_1sdiff_line ();
  28.  
  29. /* Next line number to be printed in the two input files.  */
  30. static int next0, next1;
  31.  
  32. /* Print the edit-script SCRIPT as a sdiff style output.  */
  33.  
  34. void
  35. print_sdiff_script (script)
  36.      struct change *script;
  37. {
  38.   begin_output ();
  39.  
  40.   next0 = next1 = - files[0].prefix_lines;
  41.   print_script (script, find_change, print_sdiff_hunk);
  42.  
  43.   print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
  44. }
  45.  
  46. /* Tab from column FROM to column TO, where FROM <= TO.  Yield TO.  */
  47.  
  48. static unsigned
  49. tab_from_to (from, to)
  50.      unsigned from, to;
  51. {
  52.   FILE *out = outfile;
  53.   unsigned tab;
  54.  
  55.   if (! tab_expand_flag)
  56.     for (tab = from + TAB_WIDTH - from % TAB_WIDTH;  tab <= to;  tab += TAB_WIDTH)
  57.       {
  58.     putc ('\t', out);
  59.     from = tab;
  60.       }
  61.   while (from++ < to)
  62.     putc (' ', out);
  63.   return to;
  64. }
  65.  
  66. /*
  67.  * Print the text for half an sdiff line.  This means truncate to width
  68.  * observing tabs, and trim a trailing newline.  Returns the last column
  69.  * written (not the number of chars).
  70.  */
  71. static unsigned
  72. print_half_line (line, indent, out_bound)
  73.      const char * const *line;
  74.      unsigned indent, out_bound;
  75. {
  76.   FILE *out = outfile;
  77.   register unsigned in_position = 0, out_position = 0;
  78.   register const char
  79.     *text_pointer = line[0],
  80.     *text_limit = line[1];
  81.  
  82.   while (text_pointer < text_limit)
  83.     {
  84.       register unsigned char c = *text_pointer++;
  85.  
  86.       switch (c)
  87.     {
  88.     case '\t':
  89.       {
  90.         unsigned spaces = TAB_WIDTH - in_position % TAB_WIDTH;
  91.         if (in_position == out_position)
  92.           {
  93.         unsigned tabstop = out_position + spaces;
  94.         if (tab_expand_flag)
  95.           {
  96.             if (out_bound < tabstop)
  97.               tabstop = out_bound;
  98.             for (;  out_position < tabstop;  out_position++)
  99.               putc (' ', out);
  100.           }
  101.         else
  102.           if (tabstop < out_bound)
  103.             {
  104.               out_position = tabstop;
  105.               putc (c, out);
  106.             }
  107.           }
  108.         in_position += spaces;
  109.       }
  110.       break;
  111.  
  112.     case '\r':
  113.       {
  114.         putc (c, out);
  115.         tab_from_to (0, indent);
  116.         in_position = out_position = 0;
  117.       }
  118.       break;
  119.  
  120.     case '\b':
  121.       if (in_position != 0 && --in_position < out_bound)
  122.         if (out_position <= in_position)
  123.           /* Add spaces to make up for suppressed tab past out_bound.  */
  124.           for (;  out_position < in_position;  out_position++)
  125.         putc (' ', out);
  126.         else
  127.           {
  128.         out_position = in_position;
  129.         putc (c, out);
  130.           }
  131.       break;
  132.  
  133.     case '\f':
  134.     case '\v':
  135.       if (in_position < out_bound)
  136.         putc (c, out);
  137.       break;
  138.  
  139.     default:
  140.       {
  141.         register unsigned p = in_position;
  142.         if (textchar[c])
  143.           in_position++;
  144.         if (p < out_bound)
  145.           {
  146.         out_position = in_position;
  147.         putc (c, out);
  148.           }
  149.       }
  150.       break;
  151.  
  152.     case '\n':
  153.       return out_position;
  154.     }
  155.     }
  156.  
  157.   return out_position;
  158. }
  159.  
  160. /*
  161.  * Print side by side lines with a separator in the middle.
  162.  * NULL parameters are taken to indicate whitespace text.
  163.  * Blank lines that can easily be caught are reduced to a single newline.
  164.  */
  165.  
  166. static void
  167. print_1sdiff_line (left, sep, right)
  168.      const char * const *left;
  169.      int sep;
  170.      const char * const *right;
  171. {
  172.   FILE *out = outfile;
  173.   unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
  174.   unsigned col = 0;
  175.   int put_newline = 0;
  176.   
  177.   if (left)
  178.     {
  179.       if (left[1][-1] == '\n')
  180.     put_newline = 1;
  181.       col = print_half_line (left, 0, hw);
  182.     }
  183.  
  184.   if (sep != ' ')
  185.     {
  186.       col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
  187.       if (sep == '|' && put_newline != (right[1][-1] == '\n'))
  188.     sep = put_newline ? '/' : '\\';
  189.       putc (sep, out);
  190.     }
  191.  
  192.   if (right)
  193.     {
  194.       if (right[1][-1] == '\n')
  195.     put_newline = 1;
  196.       if (**right != '\n')
  197.     {
  198.       col = tab_from_to (col, c2o);
  199.       print_half_line (right, col, hw);
  200.     }
  201.     }
  202.  
  203.   if (put_newline)
  204.     putc ('\n', out);
  205. }
  206.  
  207. /* Print lines common to both files in side-by-side format.  */
  208. static void
  209. print_sdiff_common_lines (limit0, limit1)
  210.      int limit0, limit1;
  211. {
  212.   int i0 = next0, i1 = next1;
  213.  
  214.   if (! sdiff_skip_common_lines  &&  (i0 != limit0 || i1 != limit1))
  215.     {
  216.       if (sdiff_help_sdiff)
  217.     fprintf (outfile, "i%d,%d\n", limit0 - i0, limit1 - i1);
  218.  
  219.       if (! sdiff_left_only)
  220.     {
  221.       while (i0 != limit0 && i1 != limit1)
  222.         print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
  223.       while (i1 != limit1)
  224.         print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
  225.     }
  226.       while (i0 != limit0)
  227.     print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
  228.     }
  229.  
  230.   next0 = limit0;
  231.   next1 = limit1;
  232. }
  233.  
  234. /* Print a hunk of an sdiff diff.
  235.    This is a contiguous portion of a complete edit script,
  236.    describing changes in consecutive lines.  */
  237.  
  238. static void
  239. print_sdiff_hunk (hunk)
  240.      struct change *hunk;
  241. {
  242.   int first0, last0, first1, last1, deletes, inserts;
  243.   register int i, j;
  244.  
  245.   /* Determine range of line numbers involved in each file.  */
  246.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  247.   if (!deletes && !inserts)
  248.     return;
  249.  
  250.   /* Print out lines up to this change.  */
  251.   print_sdiff_common_lines (first0, first1);
  252.  
  253.   if (sdiff_help_sdiff)
  254.     fprintf (outfile, "c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
  255.  
  256.   /* Print ``xxx  |  xxx '' lines */
  257.   if (inserts && deletes)
  258.     {
  259.       for (i = first0, j = first1;  i <= last0 && j <= last1; ++i, ++j)
  260.     print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
  261.       deletes = i <= last0;
  262.       inserts = j <= last1;
  263.       next0 = first0 = i;
  264.       next1 = first1 = j;
  265.     }
  266.  
  267.  
  268.   /* Print ``     >  xxx '' lines */
  269.   if (inserts)
  270.     {
  271.       for (j = first1; j <= last1; ++j)
  272.     print_1sdiff_line (0, '>', &files[1].linbuf[j]);
  273.       next1 = j;
  274.     }
  275.  
  276.   /* Print ``xxx  <     '' lines */
  277.   if (deletes)
  278.     {
  279.       for (i = first0; i <= last0; ++i)
  280.     print_1sdiff_line (&files[0].linbuf[i], '<', 0);
  281.       next0 = i;
  282.     }
  283. }
  284.